using System.Collections;
using Light;
using UnityEngine;

namespace Dream.ThirdLevel
{
    public class TransversePlatesPlayerController : MonoBehaviour
    {
        public float walkSpeed;
        public int jumpForce;
        public int hutiForceX;
        public int hutiForceY;
        [SerializeField] private Transform fTransform;

        [Header("物理材质")] public PhysicsMaterial2D normal;

        public PhysicsMaterial2D wall;

        [Header("音频")] public AudioClip hurtSound;

        private Animator _anim;
        private BoxCollider2D _collider;

        private int _facingDirection;

        private int _inputX;
        private int _isGroundAnim;
        private bool _isHurt;
        private int _isHurtAnim;
        private bool _isInput;
        private bool _jump;
        private int _jumpCounts;
        private float _jumpVelocity;
        private PhysicsCheck _physicsCheck;
        private Rigidbody2D _rb;
        private Vector2 _velocity;

        [Header("动画")] private int _velocityXAnim;

        private int _velocityYAnim;


        private void Awake()
        {
            _anim = GetComponent<Animator>();
            _rb = GetComponent<Rigidbody2D>();
            _physicsCheck = GetComponent<PhysicsCheck>();
            _isInput = true;
            _facingDirection = 1;
            _isHurt = false;
            _jumpCounts = 1;
            _velocityXAnim = Animator.StringToHash("velocityX");
            _velocityYAnim = Animator.StringToHash("velocityY");
            _isGroundAnim = Animator.StringToHash("isGround");
            _isHurtAnim = Animator.StringToHash("isHurt");
            _collider = GetComponent<BoxCollider2D>();
        }

        private void Update()
        {
            _inputX = LightInputManager.Instance.NormalInputX;
            _jump = LightInputManager.Instance.JumpInput;

            if (_isHurt && _physicsCheck.isGround)
                StartCoroutine(Vertigo());
            SetAnimation();

            if (_isInput)
            {
                Move();
                Jump();
            }
        }

        private void FixedUpdate()
        {
            CheckStats();
        }

        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.gameObject.CompareTag("pitfall"))
            {
                _isInput = false;
                _rb.velocity = new Vector2(0, 0);
                _rb.velocity = new Vector2(hutiForceX * -1, hutiForceY * 1);
                AudioManager.Instance.PlaySFX(hurtSound);
                StopCoroutine(Vertigo());
                StartCoroutine(SetHurt());
            }

            if (collision.gameObject.CompareTag("Water")) StartCoroutine(Wait());
        }

        public void SetAnimation()
        {
            _anim.SetFloat(_velocityXAnim, Mathf.Abs(_rb.velocity.x));
            _anim.SetFloat(_velocityYAnim, _rb.velocity.y);
            _anim.SetBool(_isGroundAnim, _physicsCheck.isGround);
            _anim.SetBool(_isHurtAnim, _isHurt);
        }

        //眩晕1秒
        private IEnumerator Vertigo()
        {
            yield return new WaitForSeconds(1.0f);
            _isHurt = false;
            _isInput = true;
        }

        private IEnumerator Wait()
        {
            yield return new WaitForSeconds(1f);
            _rb.velocity = new Vector2(0, 0);
            _rb.velocity = new Vector2(0, hutiForceY * 1.5f);
        }

        private IEnumerator SetHurt()
        {
            yield return null;
            _isHurt = true;
        }

        private void Jump()
        {
            if (_physicsCheck.isGround || _physicsCheck.touchLeftWall || _physicsCheck.touchRightWall)
                _jumpCounts = 1;
            if (_jump && _jumpCounts > 0)
                SetVelocityY();
        }

        private void Move()
        {
            CheckIfShouldFilp(_inputX);
            SetVelocityX(_inputX * walkSpeed);
        }

        private void SetVelocityY()
        {
            _rb.velocity = new Vector2(_rb.velocity.x, 0);
            _rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        }

        private void SetVelocityX(float velocity)
        {
            _jumpCounts = 0;
            _velocity.Set(velocity, _rb.velocity.y);
            _rb.velocity = _velocity;
        }

        private void CheckIfShouldFilp(int Xinput)
        {
            if (Xinput != 0 && Xinput != _facingDirection) Filp();
        }

        private void Filp()
        {
            _facingDirection *= -1;
            _rb.transform.Rotate(0f, 180f, 0f);
            fTransform.Rotate(0f, 180f, 0f);
        }

        private void CheckStats()
        {
            _collider.sharedMaterial = _physicsCheck.isGround ? normal : wall;
        }
    }
}